package com.cn.transfer.jobhandler.partner;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cn.transfer.common.constant.KeyConstant;
import com.cn.transfer.common.entity.VvAaPartner;
import com.cn.transfer.common.entity.VvSaSaleDelivery;
import com.cn.transfer.common.entity.VvSaSaleDeliveryB;
import com.cn.transfer.common.entity.jiandaoyun.JdyPurchaseOrder;
import com.cn.transfer.common.http.ApiClient;
import com.cn.transfer.common.utils.ConnmeUtils;
import com.cn.transfer.handle.impl.purchase.community.PurchaseCommunity;
import com.cn.transfer.mapper.VvAaPartnerMapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: weidian
 * @date: 2023/11/15 17:31
 * @Description: 往来单位更新
 */
@Component
public class AaPartnerUpdateXxlJob {
    @Autowired
    private VvAaPartnerMapper vvAaPartnerMapper;

    @Transactional
    @XxlJob("AaPartnerUpdate")
    public void AaPartnerUpdate() {
        List<VvAaPartner> deliveryList=vvAaPartnerMapper.byChangedateList();
        int i = 0;
        for (VvAaPartner vvAaPartner : deliveryList) {
            if (vvAaPartnerMapper.inquireIsExist(vvAaPartner.getCode())==0){
                continue;
            }
            VvAaPartner vvAaPartner1 = vvAaPartnerMapper.byOrderDocumentnumber(vvAaPartner.getId());   // 表单
            boolean b = ConnmeUtils.deepEquals(vvAaPartner1, vvAaPartner);
            if (!b) {
                if (vvAaPartnerMapper.inquireIsExist(vvAaPartner.getCode())>0){
                    // 不一致同步到视图和简道云

                    vvAaPartnerMapper.deleteId(vvAaPartner.getId());
                }
                Header[] headers = ConnmeUtils.createHeaders();
                Map<String, Object> parameterDelete = new HashMap<>();
                parameterDelete.put("app_id", "6551cc3c1e701c4474516028");
                parameterDelete.put("entry_id", "655331ce9b274035b146ec44");
                List<JdyPurchaseOrder> jdyPurchaseOrders = PurchaseCommunity.queryBased(parameterDelete, headers, vvAaPartner.getCode(),"code");
                if (jdyPurchaseOrders.size() > 0) {
                    vvAaPartnerMapper.insert(vvAaPartner);
                    Map<String, Object> parameterInster = new HashMap<>();
                    parameterInster.put("app_id", "6551cc3c1e701c4474516028");
                    parameterInster.put("entry_id", "655331ce9b274035b146ec44");
                    parameterInster.put("data_id", jdyPurchaseOrders.get(0).get_id());
                    parameterInster.put("data",ConnmeUtils.convertEntityToMap(vvAaPartner));
                    String jsonSale = JSONUtil.toJsonStr(parameterInster);
                   ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/update", headers, jsonSale);
                    i++;
                }
            }
        }
    }
}
